package com.taikang.hmp.hbusiness.diabetes.measure.download.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.taikang.hmp.hbusiness.diabetes.measure.download.utils.ConfigureUtil;


public class AppDownloadServlet  extends HttpServlet{

 private static Logger log = Logger.getLogger(AppDownloadServlet.class);
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	
	   processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	
	    processRequest(request, response);	
    }

    protected void  processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
	
	log.info("===================APP下载地址========================开始");
	
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	String agent = request.getHeader("user-agent");
	
	String returnUrl = "";
	log.info("当前设备===="+agent);
	try {
		if (agent.contains("Android")) { // android设备
			returnUrl = ConfigureUtil.getInstance().getString("android_download_url");
		} else if (agent.contains("iPhone") || agent.contains("iPad")) { // ios设备
			returnUrl = ConfigureUtil.getInstance().getString("ios_download_url");
		} else {
			returnUrl = ConfigureUtil.getInstance().getString("ios_download_url");
		}
		
	} catch (Exception e) {
		e.printStackTrace();
		log.error("ShareService ERROR , agent=" + agent+ ",Exception=" + e);
		returnUrl = "对不起，链接已失效";
	}
	log.info("APP下载地址===="+returnUrl);
	log.info("===================APP下载地址========================结束");
	/*if (agent.contains("Android")) {
	    response.sendRedirect(request.getContextPath()+returnUrl);
	 }else if(agent.contains("iPhone") || agent.contains("iPad")){
	     response.sendRedirect(request.getContextPath()+returnUrl);
	}else{
	    response.sendRedirect(returnUrl);
	}*/
	response.sendRedirect(request.getContextPath()+returnUrl);
    }
    
}
